package org.xhy.quickstarter.authority;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @Author: Xhy
 * @CreateTime: 2023-11-13 13:01
 */
public class AuthorityCache {

    private static Map<Integer, Collection<String>> map = new HashMap<>();


    /**
     * 添加权限
     * @param uId 用户id
     * @param authority 权限集合
     */
    public static void setAuthority(Integer uId, Collection<String> authority){
        map.put(uId,authority);
    }

    public static Boolean check(Integer uId,String authority){
        if (isEmpty(uId)) {
            throw new AuthorityException("未初始化权限");
        }
        return map.get(uId).contains(authority);
    }

    private static Boolean isEmpty(Integer userId){
        return !map.containsKey(userId);
    }

}
